<?php
namespace app\public_module\controller;

use \app\common\controller\Base;
use \think\Validate;
use \think\Request;
class Permissionsgroup extends Base
{
	/*
	 * 展示权限组页面
     */
      public function show_permission()
    {
        $permissionsgroup_list = db('permissionsgroup')->where('status',1)->order('id',"asc")->select();
        $this->assign('permissionsgroup_list', $permissionsgroup_list);
     	return view();
    }
    
    /*
	 * 获取全部权限
	 */
	public function get_permission()
	{
	 	return config("config_permission");
	}
    
    /*
	 * 获取选中权限组的权限
	 */
	public function get_now_permission()
	{
		$now_permissionsgroup_id = input("now_permissionsgroup_id");
	 	$permission_list = db("relation_permission_permissionsgroup")->where("permissionsgroup_id",$now_permissionsgroup_id)->select();
	 	return $permission_list;
	}
	
	/*
     * 增加权限组名
     */
    public function add_permissiongroup_name()
	{
    	$now_permissionsgroup_name = input("now_permissionsgroup_name");
    	$have_name = db("permissionsgroup")->where("description",$now_permissionsgroup_name)->find();
    	if($have_name){
	    	return json([
	            'status'=>0,
	            'msg'=>"权限组名字已存在,请重新填写!"
	        ]);
    	}else{
		   	$info = [
		   		"description"=>$now_permissionsgroup_name
		   	];
			$res = db('permissionsgroup')->insert($info);
			if ($res) {
		    	return json([
		            'status'=>1,
		            'msg'=>"添加成功"
		        ]);
		    } else {
		        return json([
		            'status'=>0,
		            'msg'=>"添加失败"
		        ]);
		    }
    	}
	}
	
	/*
     * 更新权限组
     */
    public function update_permission()
	{
    	$now_permissionsgroup = input("now_permissionsgroup");
    	$permissions = Request::instance()->only(['permissions']);
    	db('relation_permission_permissionsgroup')->where('permissionsgroup_id',$now_permissionsgroup)->delete();
    	foreach ($permissions["permissions"] as $k => $v){ 
    		$info = ['permission' => $v, 'permissionsgroup_id' => $now_permissionsgroup];
		    db('relation_permission_permissionsgroup')->insert($info);
		} 
		return true;
	}
    
    /*
	 * 根据ID删除权限组
	 */
    public function delete_permission()
    {
        $id = input("param.id");
        $res = db('permissionsgroup')->where('id', $id)->update(['status' => "0"]);
        if ($res) {
        	$status = 1;
        	$msg = "删除成功";
        } else {
        	$status = 0;
        	$msg = "删除失败";
        }
        $return = [
        	"status" => $status
        	,"res" => $id
        	,"msg" => $msg
        ];
        return $return;
    }
    
    
    /*
	 * 展示已删除的权限组页面
     */
      public function restore_permission()
    {
        $permissionsgroup_list = db('permissionsgroup')->where('status',0)->order('id',"asc")->select();
        $this->assign('permissionsgroup_list', $permissionsgroup_list);
     	return view();
    }
    
    /*
	 * 根据ID恢复权限组
	 */
    public function confirm_restore()
    {
        $id = input("param.id");
        $res = db('permissionsgroup')->where('id', $id)
            ->update(['status' => "1"]);
        if ($res) {
            $status = 1;
            $msg = "恢复成功";
        } else {
            $status = 0;
            $msg = "恢复失败";
        }
        $return = [
            "status" => $status
            ,"res" => $id
            ,"msg" => $msg
        ];
        return $return;
    }
}
?>